package com.aube.plugin.episode;

import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.BitmapFactory;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.aube.plugin.origin.R;
import com.huyn.baseframework.ImageLoader;
import com.huyn.baseframework.dynamicload.DLLayout;
import com.huyn.baseframework.dynamicload.IPositionManager;
import com.huyn.baseframework.model.Category;
import com.huyn.baseframework.utils.StringUtils;
import com.huyn.baseframework.utils.SysUtil;

import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.concurrent.atomic.AtomicBoolean;

/**
 * Created by huyaonan on 16/5/26.
 */
public class VideoListItem extends DLLayout {

    private AtomicBoolean mAnimated = new AtomicBoolean(false);
    private Category mCategory;

    private View mImgGroup, mDesGroup;
    private ImageView mImg;
    private TextView mNo, mTitle, mContent;

    private int mWidth=0;
    private int mItemWidth=0, mItemHeight=0;

    public VideoListItem(Context context) {
        this(context, null);
    }
    public VideoListItem(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }
    public VideoListItem(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public void initViews() {
        inflate(R.layout.item_video_horizontal, this, true);

        mImgGroup = findViewById(R.id.video_img_group);
        mDesGroup = findViewById(R.id.video_des_layout);
        mImg = (ImageView) findViewById(R.id.video_img);
        mNo = (TextView) findViewById(R.id.video_des);
        mTitle = (TextView) findViewById(R.id.video_title);
        mContent = (TextView) findViewById(R.id.video_played);
    }

    @Override
    public void fillData(Object obj) {
        mWidth = SysUtil.getAbsoluteScreenWidth(context);
        mItemWidth = mWidth/3;
        mItemHeight = mItemWidth*9/16;

        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) mImgGroup.getLayoutParams();
        params.width = mItemWidth;
        params.height = mItemHeight;

        if(obj == null || !(obj instanceof LinkedHashMap))
            return;
        final VideoModel item = new VideoModel();

        LinkedHashMap<String, Object> lhm = (LinkedHashMap<String, Object>) obj;
        Iterator<String> keyIterator = lhm.keySet().iterator();
        while (keyIterator.hasNext()) {
            String key = keyIterator.next();
            Object value = lhm.get(key);

            item.setItem(key, value);
        }

        mImg.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.bk_default));
        mTitle.setText(item.videoTitle);
        if(StringUtils.isBlank(item.subtitle)) {
            mContent.setVisibility(View.GONE);
        } else {
            mContent.setVisibility(View.VISIBLE);
            mContent.setText(item.subtitle);
        }
        mNo.setText(item.videoDesc);
        ImageLoader.getInstance(context).fillImage(mImg, item.pic, 400, 400);
        setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(item.isLegal()) {
                    if (mBridge != null)
                        mBridge.onClickToVideo(item.toChannelDetail(), mCategory);
                }
            }
        });
    }

    public void initData(Category category) {
        mCategory = category;
    }

    @Override
    public void notifyAnim(int animDelay) {
        if(mAnimated.get())
            return;
        mAnimated.set(true);
        setVisibility(View.VISIBLE);

        mImgGroup.setAlpha(0f);
        ObjectAnimator alphaAnim = ObjectAnimator.ofFloat(mImgGroup, "alpha", 0f, 1f);
        alphaAnim.setDuration(400);
        if(animDelay > 0)
            alphaAnim.setStartDelay(animDelay);

        mTitle.setAlpha(0f);
        ValueAnimator titleAnim = ValueAnimator.ofFloat(0, 1);
        titleAnim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float value = (float) animation.getAnimatedValue();
                mTitle.setAlpha(value);
                mTitle.setTranslationX((1-value)*mWidth/3);
            }
        });
        titleAnim.setDuration(400);
        if(animDelay > 0)
            titleAnim.setStartDelay(animDelay);

        mContent.setAlpha(0f);
        ValueAnimator desAnim = ValueAnimator.ofFloat(0, 1);
        desAnim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float value = (float) animation.getAnimatedValue();
                mContent.setAlpha(value);
                mContent.setTranslationX((1-value)*mWidth/3);
            }
        });
        desAnim.setDuration(400);
        desAnim.setStartDelay(animDelay + 100);

        alphaAnim.start();
        titleAnim.start();
        desAnim.start();
    }

    @Override
    public void registToManager(IPositionManager manager) {
        if(manager == null)
            return;
        mAnimated.set(false);
        manager.add(this);
        setVisibility(View.INVISIBLE);
    }

    @Override
    public void unreistFromManager(IPositionManager manager) {
        if(manager == null)
            return;
        manager.remove(this);
    }

    @Override
    public int[] getLocations() {
        int[] locs = new int[2];
        getLocationOnScreen(locs);
        return locs;
    }

}
